package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderDetailEntity3;
import com.sgb.kjwl.model.entity.pick.TenderInfoMainEntity3;
import com.sgb.link.view.widget.ExpandedTextView;
import com.swgk.core.recyclerview.secondary.CommonHolder;
import com.swgk.core.util.DateUtils;

import java.math.BigDecimal;

/**
 * Created by wj on 2021/2/5 0005
 * Description：
 */
public class TenderDetailsAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private TenderInfoMainEntity3 tenderInfoMainEntity3;
    private TenderDetailEntity3 tenderDetailEntity3;
    protected OnItemClick onItemClick;

    public TenderDetailsAdapter(Context context, TenderDetailEntity3 tenderDetailEntity3) {
        this.mContext = context;
        this.tenderDetailEntity3 = tenderDetailEntity3;
        this.tenderInfoMainEntity3 = tenderDetailEntity3.getTenderInfoMainEntity3();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = CommonHolder.get(mContext, parent, R.layout.tender_details_item);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        initItem((CommonHolder) holder, position);

    }

    private void initItem(CommonHolder holder, int position) {
        View itemView = holder.getItem();

        switch (position) {
            case 0:
                chooseShowItem(itemView, tenderInfoMainEntity3.getName(), "招标名称");
                break;
            case 1:
                chooseShowItem(itemView, tenderInfoMainEntity3.getProjectName(), "项目名称");
                break;
            case 2:
                if (tenderInfoMainEntity3.getBiddingOrganizationForm() == 1) {    //"招标组织形式 1-自行组织 2-委托招标"
                    chooseShowItem(itemView, tenderInfoMainEntity3.getComName(), "采购单位");
                } else if (tenderInfoMainEntity3.getBiddingOrganizationForm() == 2) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getAgencyName(), "采购单位");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 3:
                if (tenderInfoMainEntity3.getBiddingOrganizationForm() == 2) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getAgencyName(), "代理机构");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 4:
                if (TextUtils.isEmpty(tenderInfoMainEntity3.getFullAddress())) {
                    itemView.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
                } else {
                    itemView.findViewById(R.id.ll_full_address).setVisibility(View.VISIBLE);

                    //1.1、商品/材料：收货地址
                    //1.2、机械租赁、设备租赁：工作地址
                    //1.3、劳务分包、专业工程、勘察设计、建筑设计、工程监理、工程造价咨询、工程总承包：项目地址
                    //1.4、车辆租赁、房产租赁：使用地址

                    // 2, "劳务分包" 3, "商品/材料" 4, "专业工程" 6, "勘察设计" 7, "建筑设计" 8, "工程监理" 9, "工程造价咨询" 10, "工程总承包"
                    // 1, "机械租赁" 5, "设备租赁" 11, "房产租赁" 12, "车辆租赁"

                    // 14、土地产权转让：土地地址
                    // 15、房产转让、16机械转让、17设备转让、18车辆转让：所在地址
                    // 19、物流货运：发货地址
                    // 13、PPP项目：项目地址   20 其他：企业地址

                    int type = tenderInfoMainEntity3.getType();
                    TextView addressTitle = itemView.findViewById(R.id.address_title);
                    if (type == 12 || type == 11) {
                        addressTitle.setText("使用地址");
                    } else if (type == 3) {
                        addressTitle.setText("收货地址");
                    } else if (type == 5 || type == 1) {
                        addressTitle.setText("工作地址");
                    } else if (type == 14) {
                        addressTitle.setText("土地地址");
                    } else if (type == 15 || type == 16 || type == 17 || type == 18 || type == 20) {
                        addressTitle.setText("所在地址");
                    } else if (type == 19) {
                        addressTitle.setText("发货地址");
                    } else {
                        addressTitle.setText("项目地址");
                    }
                    String lat = tenderInfoMainEntity3.getLat();
                    String lng = tenderInfoMainEntity3.getLng();
                    String address = tenderInfoMainEntity3.getFullAddress();
                    TextView addressContent = itemView.findViewById(R.id.address_content);
                    addressContent.setText(address);
                    ImageView imageViewMap = itemView.findViewById(R.id.pick_location_map);
                    if (!TextUtils.isEmpty(lat) && !TextUtils.isEmpty(lng)) {
                        imageViewMap.setVisibility(View.VISIBLE);
                    }

                    String distance = TextUtils.isEmpty(tenderDetailEntity3.getDistance()) ? "" : tenderDetailEntity3.getDistance();
                    TextView tvDistance = itemView.findViewById(R.id.tv_distance);
                    if (!TextUtils.isEmpty(distance)) {
                        tvDistance.setText(distance + "km");
                    } else {
                        tvDistance.setText("");
                    }
                    tvDistance.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClick.onDistanceClick(lat, lng, address);
                        }
                    });
                    imageViewMap.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClick.onMapClick(lat, lng, address);
                        }
                    });
                }
                itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                itemView.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
                itemView.findViewById(R.id.layout_time).setVisibility(View.GONE);
                break;
            case 5:
                String tVContent = "";
                if (tenderInfoMainEntity3.getMoney() > 0) {
                    BigDecimal bd = new BigDecimal(String.valueOf(tenderInfoMainEntity3.getMoney()));
                    tVContent = bd.stripTrailingZeros().toPlainString() + "元";
                } else {
                    tVContent = "0元";
                }
                chooseShowItem(itemView, tVContent, "预算金额");
                break;
            case 6:
                if (TextUtils.isEmpty(tenderDetailEntity3.getTenderInfoMainEntity3().getRemark())) {
                    itemView.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
                } else {
                    itemView.findViewById(R.id.ll_survey_view).setVisibility(View.VISIBLE);
                    TextView surveyView = itemView.findViewById(R.id.survey_view);
                    TextView expandView = itemView.findViewById(R.id.expand_view);
                    surveyView.setText(tenderInfoMainEntity3.getRemark());
                    new ExpandedTextView().showTextPick(surveyView, expandView);
                }
                itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                itemView.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
                itemView.findViewById(R.id.layout_time).setVisibility(View.GONE);
                break;
            case 7:
                if (tenderInfoMainEntity3.getMoreContactList() != null && tenderInfoMainEntity3.getMoreContactList().size() > 0) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getContacts() + " " + tenderInfoMainEntity3.getTel(), "联系人1");
                }else{
                    chooseShowItem(itemView, tenderInfoMainEntity3.getContacts() + " " + tenderInfoMainEntity3.getTel(), "联系人");
                }
                break;
            case 8:
                if (tenderInfoMainEntity3.getMoreContactList() != null && tenderInfoMainEntity3.getMoreContactList().size() > 0) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getMoreContactList().get(0) + " " + tenderInfoMainEntity3.getMoreTelList().get(0), "联系人2");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 9:
                if (tenderInfoMainEntity3.getMoreContactList() != null && tenderInfoMainEntity3.getMoreContactList().size() > 1) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getMoreContactList().get(1) + " " + tenderInfoMainEntity3.getMoreTelList().get(1), "联系人3");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 10:
                if (tenderInfoMainEntity3.getMoreContactList() != null && tenderInfoMainEntity3.getMoreContactList().size() > 2) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getMoreContactList().get(2) + " " + tenderInfoMainEntity3.getMoreTelList().get(2), "联系人4");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 11:
                if (tenderInfoMainEntity3.getMoreContactList() != null && tenderInfoMainEntity3.getMoreContactList().size() > 3) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getMoreContactList().get(3) + " " + tenderInfoMainEntity3.getMoreTelList().get(3), "联系人5");
                } else {
                    goneAllView(itemView);
                }
                break;
//            case 10:
//                chooseShowItem(itemView, tenderInfoMainEntity3.getEmail(), "邮箱");
//                break;
            case 12:
                //报名即报价时，报名时间、投标时间合起来展示为报名&报价时间
                //1、待招标—投标时间(投标开始-投标结束),2、招标中—投标时间(投标开始-投标结束),3、评标中—开标时间,4、招标结束—定标时间,5、废标,6、再次招标—投标时间,7、流标,8、待报名,9、报名中 -报名时间(报名开始-报名结束),10、资格审查中
                String ApplyStartTime = tenderInfoMainEntity3.getApplyStartTime();
                String ApplyEndTime = tenderInfoMainEntity3.getApplyEndTime();
                String StartTime = tenderInfoMainEntity3.getStartTime();
                String EndTime = tenderInfoMainEntity3.getEndTime();
                String BidTime = tenderInfoMainEntity3.getBidTime();
                String WinningTime = tenderInfoMainEntity3.getWinningTime();

                if (TextUtils.isEmpty(ApplyStartTime) && TextUtils.isEmpty(ApplyEndTime) && TextUtils.isEmpty(StartTime) &&
                        TextUtils.isEmpty(EndTime) && TextUtils.isEmpty(BidTime) && TextUtils.isEmpty(WinningTime)) {
                    itemView.findViewById(R.id.layout_time).setVisibility(View.GONE);
                } else {
                    itemView.findViewById(R.id.layout_time).setVisibility(View.VISIBLE);

                    TextView tvStartTimeTag = itemView.findViewById(R.id.tv_start_time_tag);
                    TextView tvStartTime = itemView.findViewById(R.id.tv_start_time);
//                    TextView tvEndTimeT = itemView.findViewById(R.id.tv_end_time_t);

                    if (tenderInfoMainEntity3.getState() == 9) { //我要报名
                        if (!TextUtils.isEmpty(ApplyStartTime) || !TextUtils.isEmpty(ApplyEndTime)) {
                            tvStartTimeTag.setText("报名时间");
                            timeViewUpdate(ApplyStartTime, tvStartTime, ApplyEndTime);

                        }
                    } else if (tenderInfoMainEntity3.getState() == 6) {
                        if (!TextUtils.isEmpty(StartTime) || !TextUtils.isEmpty(EndTime)) {
                            tvStartTimeTag.setText("投标时间");
                            timeViewUpdate(StartTime, tvStartTime, EndTime);

                        }
                    } else if (tenderInfoMainEntity3.getState() == 1) {
                        if (!TextUtils.isEmpty(StartTime) || !TextUtils.isEmpty(EndTime)) {
                            tvStartTimeTag.setText("投标时间");
                            timeViewUpdate(StartTime, tvStartTime, EndTime);

                        }
                    } else if (tenderInfoMainEntity3.getState() == 2) {
                        if (!TextUtils.isEmpty(StartTime) || !TextUtils.isEmpty(EndTime)) {
                            tvStartTimeTag.setText("投标时间");
                            timeViewUpdate(StartTime, tvStartTime, EndTime);

                        }
                    } else if (tenderInfoMainEntity3.getState() == 3) {
                        if (!TextUtils.isEmpty(BidTime)) {

                            tvStartTimeTag.setText("开标时间");
//                            tvEndTimeT.setVisibility(View.GONE);

                            String st = DateUtils.stampToDatePick(BidTime);
                            tvStartTime.setText(st);

                        }
                    } else if (tenderInfoMainEntity3.getState() == 4) {
                        if (!TextUtils.isEmpty(WinningTime)) {

                            tvStartTimeTag.setText("定标时间");
//                            tvEndTimeT.setVisibility(View.GONE);

                            String st = DateUtils.stampToDatePick(WinningTime);
                            tvStartTime.setText(st);
                            tvStartTime.setTextColor(Color.parseColor("#969696"));

                        }
                    }

                    if (tenderInfoMainEntity3.getState() == 9||tenderInfoMainEntity3.getState() == 2) {
                        if(tenderInfoMainEntity3.getVerticalTypeValue() == 3){
                            tvStartTimeTag.setText("报名&报价  时间");
                        }
                    }

                    if (TextUtils.isEmpty(tvStartTime.getText())) {
                        itemView.findViewById(R.id.layout_time).setVisibility(View.GONE);
                    }
                }

                itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                itemView.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
                itemView.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
                break;
            case 13:
                if (tenderInfoMainEntity3.getState() == 3) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getBidMeetingPlatform(), "开标会议软件");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 14:
                if (tenderInfoMainEntity3.getState() == 3) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getBidMeetingNo(), "开标会议号");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 15:
                if (tenderInfoMainEntity3.getState() == 3) {
                    chooseShowItem(itemView, tenderInfoMainEntity3.getBidAddress(), "开标地点");
                } else {
                    goneAllView(itemView);
                }
                break;
            case 16:
                chooseShowItem(itemView, tenderInfoMainEntity3.getRuleContent(), "投标人限定");
                break;

        }

    }

    private void timeViewUpdate(String StartTime, TextView tvStartTime, String EndTime) {

        if (!TextUtils.isEmpty(StartTime)) {
            String startTime = DateUtils.stampToDatePick(StartTime);

            if (!TextUtils.isEmpty(EndTime)) {
                String endTime = DateUtils.stampToDatePick(EndTime);
                tvStartTime.setText(startTime + "-" + endTime);
            }

        } else {
            tvStartTime.setVisibility(View.GONE);
        }

    }

    private void chooseShowItem(View view, String itemValue, String itemTitle) {
        TextView tVTitle = view.findViewById(R.id.item_title);
        TextView tVContent = view.findViewById(R.id.item_content);

        if (!TextUtils.isEmpty(itemValue)) {
            view.findViewById(R.id.item_layout).setVisibility(View.VISIBLE);
            if (itemTitle.contains("联系人")) {
                SpannableString span = new SpannableString(itemValue);
                ClickableSpan click = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        String tel  = itemValue.substring(itemValue.length() - 11, itemValue.length());
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + tel);
                        intent.setData(data);
                        mContext.startActivity(intent);
                    }
                    @Override
                    public void updateDrawState(TextPaint ds) {
                        ds.setUnderlineText(false);
                    }
                };
                span.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_0286DF)), itemValue.length() - 11, itemValue.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                span.setSpan(click, itemValue.length() - 11, itemValue.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tVContent.append(span);
                tVContent.setMovementMethod(LinkMovementMethod.getInstance());
            }else{
                tVContent.setText(itemValue);
            }
            tVTitle.setText(itemTitle);
        } else {
            view.findViewById(R.id.item_layout).setVisibility(View.GONE);
        }
        goneView(view);
    }

    private void goneView(View view) {
        view.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
        view.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
        view.findViewById(R.id.layout_time).setVisibility(View.GONE);
    }

    private void goneAllView(View view) {
        view.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
        view.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
        view.findViewById(R.id.layout_time).setVisibility(View.GONE);
        view.findViewById(R.id.item_layout).setVisibility(View.GONE);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return 17;
    }

    public interface OnItemClick {
        void onMapClick(String lat, String lag, String address);
        void onDistanceClick(String lat, String lag, String address);
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}
